#pragma once

#include <mysql/mysql.h>
#include <sstream>
#include <map>
#include <string>
#include <iostream>

class UserModule {
public:
    static bool Register(const std::string& body, std::string* result) {
        auto data = ParseBody(body);
        std::string username = data["username"];
        std::string password = data["password"];
        std::string confirm = data["confirm_password"];

        if (password != confirm) {
            *result = "<h3>两次密码不一致</h3>";
            return false;
        }

        MYSQL* conn = mysql_init(nullptr);
        if (!mysql_real_connect(conn, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, NULL, 0)) {
            *result = "<h3>数据库连接失败</h3>";
            return false;
        }

        std::string query = "INSERT INTO oj_user(username, password) VALUES('" + username + "','" + password + "')";
        if (mysql_query(conn, query.c_str()) == 0) {
            *result = "<h3>注册成功！<a href='/login.html'>点击登录</a></h3>";
            mysql_close(conn);
            return true;
        } else {
            *result = "<h3>注册失败，用户名已存在</h3>";
            mysql_close(conn);
            return false;
        }
    }

    static bool Login(const std::string& body, std::string* result) {
        auto data = ParseBody(body);
        std::string username = data["username"];
        std::string password = data["password"];

        MYSQL* conn = mysql_init(nullptr);
        if (!mysql_real_connect(conn, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, NULL, 0)) {
            *result = "<h3>数据库连接失败</h3>";
            return false;
        }

        std::string query = "SELECT * FROM oj_user WHERE username='" + username + "' AND password='" + password + "'";
        mysql_query(conn, query.c_str());
        MYSQL_RES* res = mysql_store_result(conn);

        if (mysql_num_rows(res) > 0) {
            *result = "<h3>登录成功！欢迎回来 " + username + "</h3><a href='/all_questions'>点击进入题库</a>";
            mysql_free_result(res);
            mysql_close(conn);
            return true;
        } else {
            *result = "<h3>用户名或密码错误</h3>";
            mysql_free_result(res);
            mysql_close(conn);
            return false;
        }
    }

private:
    static std::map<std::string, std::string> ParseBody(const std::string& body) {
        std::map<std::string, std::string> res;
        std::istringstream ss(body);
        std::string kv;
        while (getline(ss, kv, '&')) {
            size_t pos = kv.find('=');
            if (pos != std::string::npos) {
                std::string key = kv.substr(0, pos);
                std::string val = kv.substr(pos + 1);
                res[key] = val;
            }
        }
        return res;
    }

    static const std::string oj_user;
    static const std::string host;
    static const std::string user;
    static const std::string passwd;
    static const std::string db;
    static const int port;
};

// 定义静态成员变量
const std::string UserModule::oj_user = "oj_user";
const std::string UserModule::host = "127.0.0.1";
const std::string UserModule::user = "oj_client";
const std::string UserModule::passwd = "123456";
const std::string UserModule::db = "oj";
const int UserModule::port = 3306;
